package com.ghxxzx.daglcustom.controller.instance;

import com.ghxxzx.daglcustom.utils.ActivitiTools;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.tmsps.ne4spring.utils.wrapper.WrapMapper;
import org.tmsps.ne4spring.utils.wrapper.Wrapper;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
public class ActivitiInstanceController {

	@Autowired
	private RuntimeService runtimeService;

	@RequestMapping("/activitiInstanceList")
	public List<Map<String, Object>> activitiInstanceList(String name) {
		// 创建查询对象
		ProcessInstanceQuery processInstanceQuery = runtimeService.createProcessInstanceQuery();
		if (name != null && !"".equals(name)) {
			processInstanceQuery.processInstanceNameLike("%" + name + "%");
		}

		List<ProcessInstance> list = processInstanceQuery.list();
		return ActivitiTools.turnProcessInstances(list);
	}

	@GetMapping("/instance/suspend")
	public Wrapper<String> suspend(@Valid String processInstanceId) {
		runtimeService.suspendProcessInstanceById(processInstanceId);
		return WrapMapper.ok("挂起成功");
	}

	@GetMapping("/instance/gorun")
	public Wrapper<String> gorun(@Valid String processInstanceId) {
		runtimeService.activateProcessInstanceById(processInstanceId);
		return WrapMapper.ok("重新激活成功");
	}
}
